﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Data;
using System.Net;
using System.Text;

namespace KlirWebApplicationTest
{
    /// <summary>
    /// Summary description for KlirWS
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class KlirWS : System.Web.Services.WebService
    {
        public string loginresult="0";
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
        public string Login(string username,string password)
        {
            
            try
            {
                if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
                {
                    DataClasses1DataContext dc = new DataClasses1DataContext();


                    var q = dc.GETCUSTCODE(username, password);
                    loginresult = q.ReturnValue != null ? q.ReturnValue.ToString() : null;
                   
                }
            }
            catch (Exception)
            {

                throw;
            }
            return loginresult;

        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
        public void SendEmail(string email)
        {
            MailMessage mailObj = new MailMessage("admin@klir.co.il", email);
            SmtpClient SMTPServer = new SmtpClient("localhost");
            try
            {
                //SMTPServer.
                SMTPServer.Send(mailObj);
            }
            catch (Exception ex)
            {
               // Label1.Text = ex.ToString();
            }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
        public string GetUserDetails()
        {
            List<USERS> users = new List<USERS>();
            List<List<USERS>> Jsonusers = new List<List<USERS>>();
            DataClasses1DataContext dc = new DataClasses1DataContext();
            var q = (from item in dc.GetAllUsers()
                     select item).ToList();
            foreach (var item in q)
            {
                USERS usersIn = new USERS();
                usersIn.FULL_NAME = item.FULL_NAME != null ? item.FULL_NAME.ToString() : "";
                usersIn.MOBILE = item.MOBILE != null ? item.MOBILE.ToString() : "";
                usersIn.EMAIL = item.EMAIL != null ? item.EMAIL.ToString() : "";
                usersIn.CONTACT_ID = item.CONTACT_ID.ToString();
                usersIn.CUST_CODE = item.CUST_CODE != null ? item.CUST_CODE.ToString() : "";
                usersIn.CAN_CONFIRM_ORDER = item.CAN_CONFIRM_ORDER != null ? item.CAN_CONFIRM_ORDER.ToString() : ""; 
                usersIn.CAN_CRE_ORDER = item.CAN_CRE_ORDER != null ? item.CAN_CRE_ORDER.ToString() : "";
                usersIn.CAN_READ_ACCOUNT = item.CAN_READ_ACCOUNT != null ? item.CAN_READ_ACCOUNT.ToString() : "";
                usersIn.CAN_READ_ALL_CUST_GRP_ORDERS = item.CAN_READ_ALL_CUST_GRP_ORDERS != null ? item.CAN_READ_ALL_CUST_GRP_ORDERS.ToString() : "";
                usersIn.CAN_READ_ALL_CUST_ORDERS = item.CAN_READ_ALL_CUST_ORDERS != null ? item.CAN_READ_ALL_CUST_ORDERS.ToString() : "";
                usersIn.USER_NAME = item.USER_NAME != null ? item.USER_NAME.ToString() : "";
                users.Add(usersIn);
            }
            var serializer = new JavaScriptSerializer();
            var serializedResult = serializer.Serialize(users);
            return serializedResult;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetUserInfo(string CUST_CODE)
        {
            string serializedResult = string.Empty;

            DataClasses1DataContext dc = new DataClasses1DataContext();
            var query = from item in dc.GetUserInfo(CUST_CODE)
                        select item;

            var serializer = new JavaScriptSerializer();
            serializedResult = serializer.Serialize(query);
            return serializedResult;
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetUserCategories(string CUST_CODE)
        {
            string serializedResult = string.Empty;
            List<DataSet> users = new List<DataSet>();
          
                DataClasses1DataContext dc = new DataClasses1DataContext();
                var query = from item in dc.GetAllItemsForUser(CUST_CODE)
                         select item;

            var serializer = new JavaScriptSerializer();
            serializedResult = serializer.Serialize(query);
            return serializedResult;

        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetUserOrders(string CUST_CODE)
        {
            string serializedResult = string.Empty;
            List<DataSet> users = new List<DataSet>();

            DataClasses1DataContext dc = new DataClasses1DataContext();
            var query = from item in dc.GetUsersOrders(CUST_CODE)
                        select item;

            var serializer = new JavaScriptSerializer();
            serializedResult = serializer.Serialize(query);
            return serializedResult;

        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string SendMail(string CUST_CODE,string email, string subject, string fullname, string phone, string content, string inerest)
        {
            string success = string.Empty;


            StringBuilder sb = new StringBuilder();
            sb.Append("<table style='direction:rtl;border:1px solid #4b4a4a;'>");
            sb.Append("<tr><td>נושא</td><td>" + subject + "</td></tr>");
            sb.Append("<tr><td>Email</td><td>" + email + "</td></tr>");
            sb.Append("<tr><td>שם מלא</td><td>" + fullname + "</td></tr>");
            sb.Append("<tr><td>טלפון</td><td>" + phone + "</td></tr>");
            sb.Append("<tr><td>תוכן</td><td>" + content + "</td></tr>");
            sb.Append("<tr><td>מעוניין בתוכן פרסומי</td><td>" + inerest + "</td></tr>");
            sb.Append("<table>");
            try
            {
                SmtpClient smtpClient = new SmtpClient();
                NetworkCredential basicCredential =
                    new NetworkCredential("crm", "crm");
                MailMessage message = new MailMessage();
                MailAddress fromAddress = new MailAddress("crm@klir.co.il");

                smtpClient.Host = "213.8.170.59";
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = basicCredential;

                message.From = fromAddress;
                message.Subject = subject;
                //Set IsBodyHtml to true means you can send HTML email.
                message.IsBodyHtml = true;
                message.Body = sb.ToString();
                //message.To.Add("orders@klir.co.il");
                message.To.Add("assafb@one1.co.il");
                try
                {
                    smtpClient.Send(message);
                    success = "succes sending mail..";
                }
                catch (Exception ex)
                {
                    success = ex.ToString() +"     No sending email success";
                    //Error, could not send the message
                   // Response.Write(ex.Message);
                }
            }
            catch (Exception)
            {
                success = "No sending email success";
                throw;
            }


            return success;
        }
    }
}
